define(['jquery', 'bootstrap', 'backend', 'table', 'form','echarts', 'echarts-theme', 'template'], function ($, undefined, Backend, Table, Form, Echarts, undefined, Template) {
    var Controller = {
        new: function () {

            var myChart2=Echarts.init(document.getElementById('echart'), 'walden');
            var option2 = {
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross',
                        crossStyle: {
                            color: '#999'
                        }
                    }
                },
                toolbox: {
                    feature: {
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar']},
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                legend: {
                    data: ['新增客户数量', '回款客户数量']
                },
                xAxis: [
                    {
                        type: 'category',
                        data: Config.result.date,
                        axisPointer: {
                            type: 'shadow'
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        name: '数量',

                        axisLabel: {
                            formatter: '{value} 人'
                        }
                    },
                    {
                        type: 'value',
                        name: '数量',

                        axisLabel: {
                            formatter: '{value} 人'
                        }
                    }
                ],
                series: [
                    {
                        name: '新增客户数量',
                        type: 'bar',
                        data: Config.result.add_count
                    },
                    {
                        name: '回款客户数量',
                        type: 'bar',
                        data:  Config.result.trade_count
                    }
                ]
            };

            // 使用刚指定的配置项和数据显示图表。
            myChart2.setOption(option2);
            $(window).resize(function () {
                myChart2.resize();
            });
            var configids = Config.ids;
            $('.sender').on('change', function() {
                var ids = $(this).val();
                if(ids == 0){
                    $('#c-external_staff_id').text('<option value="0" id="external_staff_id">请选择</option>')
                    return false;
                }
                $.post('qingdongwms/statistics/customer/staffes',
                    {
                        'typeid':ids
                    },
                    function (data) {
                        $('#c-external_staff_id').html("");
                        for(var i in data){
                            if(configids && configids == i){
                                $('#c-external_staff_id').append('<option value="'+i+'" selected>'+data[i]+'</option>')
                            }else{
                                $('#c-external_staff_id').append('<option value="'+i+'">'+data[i]+'</option>')
                            }

                        }
                        $('select').selectpicker('refresh');
                    }
                );
            });
            $('.sender').trigger('change');
            // Controller.api.bindevent();
            Form.api.bindevent($("form.forms"), function(data,ret){
                //成功的回调,如果返回false,将不会出现提示消息
                window.location.href = ret.url;
                //return false;
            }, function(data, ret){
                //失败的回调,如果返回false,将不会出现提示消息
                //return false;
            }, function(success, error){
                console.log(error)
                console.log(success)
                window.location.href = window.location.pathname+'?'+$(this).serialize();
                return false;
                //如果返回falsle,将阻止表单的提交
                //return false;
                //如果处理成功后我们可以使用Form.api.submit(this, success, error);再次提交表单
            });

        },
        follow: function () {

            var myChart2=Echarts.init(document.getElementById('echart'), 'walden');
            var option2 = {
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross',
                        crossStyle: {
                            color: '#999'
                        }
                    }
                },
                toolbox: {
                    feature: {
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar']},
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                legend: {
                    data: ['员工跟进客户数', '老客跟进次数']
                },
                xAxis: [
                    {
                        type: 'category',
                        data: Config.result.date,
                        axisPointer: {
                            type: 'shadow'
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        name: '数量',

                        axisLabel: {
                            formatter: '{value} 人'
                        }
                    },
                    {
                        type: 'value',
                        name: '数量',

                        axisLabel: {
                            formatter: '{value} 人'
                        }
                    }
                ],
                series: [
                    {
                        name: '员工跟进客户数',
                        type: 'bar',
                        data: Config.result.add_count
                    },
                    {
                        name: '老客跟进次数',
                        type: 'bar',
                        data: Config.result.trade_count
                    }
                ]
            };

            // 使用刚指定的配置项和数据显示图表。
            myChart2.setOption(option2);
            $(window).resize(function () {
                myChart.resize();
            });
            var configids = Config.ids;
            $('.sender').on('change', function() {
                var ids = $(this).val();
                if(ids == 0){
                    $('#c-external_staff_id').text('<option value="0" id="external_staff_id">请选择</option>')
                    return false;
                }
                $.post('qingdongwms/statistics/customer/staffes',
                    {
                        'typeid':ids
                    },
                    function (data) {
                        $('#c-external_staff_id').html("");
                        for(var i in data){
                            if(configids && configids == i){
                                $('#c-external_staff_id').append('<option value="'+i+'" selected>'+data[i]+'</option>')
                            }else{
                                $('#c-external_staff_id').append('<option value="'+i+'">'+data[i]+'</option>')
                            }
                        }
                        $('select').selectpicker('refresh');
                    }
                );
            });
            $('.sender').trigger('change');
            Form.api.bindevent($("form.forms"), function(data,ret){
                //成功的回调,如果返回false,将不会出现提示消息
                window.location.href = ret.url;
                //return false;
            }, function(data, ret){
                //失败的回调,如果返回false,将不会出现提示消息
                //return false;
            }, function(success, error){
                console.log(error)
                console.log(success)
                window.location.href = window.location.pathname+'?'+$(this).serialize();
                return false;
                //如果返回falsle,将阻止表单的提交
                //return false;
                //如果处理成功后我们可以使用Form.api.submit(this, success, error);再次提交表单
            });

        },
        deal: function () {

            var myChart2=Echarts.init(document.getElementById('echart'), 'walden');
            var option2 = {
                title: {
                    text: 'Funnel'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: '{a} <br/>{b} : {c}人'
                },
                toolbox: {
                    feature: {
                        dataView: { readOnly: true },
                        restore: {},
                        saveAsImage: {}
                    }
                },
                legend: {
                    data: ['到访', '报价', '合同']
                },
                series: [
                    {
                        name: 'Expected',
                        type: 'funnel',
                        left: '10%',
                        width: '80%',
                        label: {
                            formatter: '{b}Expected'
                        },
                        itemStyle: {
                            opacity: 0.9
                        },
                        emphasis: {
                            label: {
                                position: 'inside',
                                formatter: '{b}Expected: {c}人'
                            }
                        },
                        data: Config.result
                    },
                    {
                        name: 'Actual',
                        type: 'funnel',
                        left: '10%',
                        width: '80%',
                        maxSize: '80%',
                        label: {
                            position: 'inside',
                            formatter: '{c}%',
                            color: '#fff'
                        },
                        itemStyle: {
                            opacity: 0.5,
                            borderColor: '#fff',
                            borderWidth: 2
                        },
                        emphasis: {
                            label: {
                                position: 'inside',
                                formatter: '{b}Actual: {c}%'
                            }
                        },
                        data: [ ],
                        // Ensure outer shape will not be over inner shape when hover.
                        z: 100
                    }
                ]
            };

            // 使用刚指定的配置项和数据显示图表。
            myChart2.setOption(option2);
            $(window).resize(function () {
                myChart2.resize();
            });
            // var configids = $('#c-external_staff_id').data('value');
            var configids = Config.ids;
            $('.sender').on('change', function() {
                var ids = $(this).val();
                if(ids == 0){
                    $('#c-external_staff_id').text('<option value="0" id="external_staff_id">请选择</option>')
                    return false;
                }
                $.post('qingdongwms/statistics/customer/staffes',
                    {
                        'typeid':ids
                    },
                    function (data) {
                        $('#c-external_staff_id').html("");
                        for(var i in data){
                            if(configids && configids == i){
                                $('#c-external_staff_id').append('<option value="'+i+'" selected>'+data[i]+'</option>')
                            }else{
                                $('#c-external_staff_id').append('<option value="'+i+'">'+data[i]+'</option>')
                            }
                        }
                        $('select').selectpicker('refresh');
                    }
                );
            });
            $('.sender').trigger('change');
            Form.api.bindevent($("form.forms"), function(data,ret){
                //成功的回调,如果返回false,将不会出现提示消息
                window.location.href = ret.url;
                //return false;
            }, function(data, ret){
                //失败的回调,如果返回false,将不会出现提示消息
                //return false;
            }, function(success, error){
                console.log(error)
                console.log(success)
                window.location.href = window.location.pathname+'?'+$(this).serialize();
                return false;
                //如果返回falsle,将阻止表单的提交
                //return false;
                //如果处理成功后我们可以使用Form.api.submit(this, success, error);再次提交表单
            });

        },
        //客户总量分析
        index: function () {


            var myChart2=Echarts.init(document.getElementById('echart'), 'walden');
            var option2 = {

                color:['#c23531'],

                legend: {
                    data: ['新增客户数', '成交客户数']
                },
                xAxis: [
                    {
                        type: 'category',
                        data: Orderdata.customerdata.date,

                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        name: '数量',
                        axisLabel: {
                            formatter: '{value} 个',
                        }
                    },
                    {
                        type: 'value',


                    }
                ],
                series: [
                    {
                        name: '新增客户数',
                        type: 'bar',
                        data: Orderdata.customerdata.num
                    },

                    {
                        name: '成交客户数',
                        type: 'bar',

                        data: Orderdata.customerdata.use
                    }
                ]
            };

            // 使用刚指定的配置项和数据显示图表。
            myChart2.setOption(option2);


        },
        //客户跟进次数分析
        record: function () {
            var myChart2=Echarts.init(document.getElementById('echart'), 'walden');
            var option2 = {

                color:['#c23531'],

                legend: {
                    data: ['跟进客户数', '跟进次数']
                },
                xAxis: [
                    {
                        type: 'category',
                        data: Orderdata.customerdata.date,

                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        name: '跟进客户数',
                        axisLabel: {
                            formatter: '{value} 个',
                        }
                    },
                    {
                        type: 'value',
                        name: '跟进次数',
                        axisLabel: {
                            formatter: '{value} 次',
                        }

                    }
                ],
                series: [
                    {
                        name: '跟进客户数',
                        type: 'bar',
                        data: Orderdata.customerdata.num
                    },

                    {
                        name: '跟进次数',
                        type: 'bar',

                        data: Orderdata.customerdata.use
                    }
                ]
            };

            // 使用刚指定的配置项和数据显示图表。
            myChart2.setOption(option2);


        },
        //客户跟进方式分析
        recordtype:function(){
            var series=[];
            $.each(Orderdata.sourcedata,function(index){
                if(index != 0){
                    series.push({type: 'line', smooth: true, seriesLayoutBy: 'row', emphasis: {focus: 'series'}});
                }

            });
            var length=Orderdata.sourcedata[0].length;
            length=length-1;
            series.push({
                type: 'pie',
                id: 'pie',
                radius: '40%',
                center: ['50%', '30%'],
                emphasis: {focus: 'data'},
                label: {
                    formatter: '{b}: {@'+Orderdata.sourcedata[0][length]+'} ({d}%)'
                },
                encode: {
                    itemName: Orderdata.sourcedata[0][0],
                    value:  Orderdata.sourcedata[0][length],
                    tooltip:  Orderdata.sourcedata[0][length]
                }
            });
            // 基于准备好的dom，初始化echarts实例
            var myChart = Echarts.init(document.getElementById('echart'), 'walden');

            // 指定图表的配置项和数据
            var  option = {
                legend: {},
                tooltip: {
                    trigger: 'axis',
                    showContent: false
                },
                dataset: {
                    source: Orderdata.sourcedata
                },
                xAxis: {type: 'category'},
                yAxis: {gridIndex: 0},
                grid: {top: '65%',height:'32%'},
                series:series
            };

            myChart.on('updateAxisPointer', function (event) {
                var xAxisInfo = event.axesInfo[0];
                if (xAxisInfo) {
                    var dimension = xAxisInfo.value + 1;
                    myChart.setOption({
                        series: {
                            id: 'pie',
                            label: {
                                formatter: '{b}: {@[' + dimension + ']} ({d}%)'
                            },
                            encode: {
                                value: dimension,
                                tooltip: dimension
                            }
                        }
                    });
                }
            });

            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form.forms"));
                // Form.api.bindevent($("form[class=form]"));

            }
        }
    };
    return Controller;
});